package graphic;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;

import Graph.Graph;
import Graph.pathway;

public class SingleCar{
	
	public int[][] graph;
	public int[][] carsOnRoads;
	public float[][] jams;
	public int number;
	private int way [] =  new int [Graph.maxVertexes];
	public int length;
	public Point point [];
	private int numThrough;
	private int current;
	double x;
	double y;
	static public double speed = 0.8;
	static public double currentSpeed = speed;
	private double tangens [] = new double [3];
	private double dx;
	private int next;
	private int FlagDeleted = 0;
	public SingleCar(Graph City, pathway optWay){
		point = City.getPoint();
		graph = City.getGraph();
		number = City.getNumber();
		carsOnRoads = City.getCarsOnRoads();
		jams = City.getJams();
		
		pathway oWay = optWay;
		for(int j = 0; j < Graph.maxVertexes; j++) way[j] = oWay.getRoute() [j];
		length = optWay.getLength();
		numThrough = 1;
		next = way[1];
		current = way[0];
		currentSpeed = speed / (float) (jams[current][next]);
		x = point[current].x;
		y = point[current].y;
		++carsOnRoads[current][next];
		if(length == 1){
			FlagDeleted = 1;
			--carsOnRoads[current][next];
		}
	}
	

	private double[] getTangens(int nextVert, double x, double y){
		double tangens [] = new double[3];
		if(point[nextVert].y >= y) tangens[2] = 1.0;
		else tangens[2] = -1.0;
		
		if (point[nextVert].x < x) tangens[1] = -1.0;
		else if (point[nextVert].x > x) tangens[1] = 1.0;
		else if(x == point[nextVert].x) {
			return tangens;
		}
		tangens [0] = ((float) ((float) point[nextVert].y - y)) / ((float) ((float) point[nextVert].x - x));
		return tangens;
	}
	
	public void paintCar(Graphics g){
		if(carsOnRoads [current][next] >= 20) jams[current][next] = 6;
		else jams[current][next] = (float) carsOnRoads[current][next] / 4 + 1f;
		currentSpeed = speed / (float) (jams[current][next]);
		Graphics2D g1 = (Graphics2D) g;
		if(numThrough < length){
			if(((x - point[next].x) * (x - point[next].x)  + (y - point[next].y) * (y - point[next].y)) < currentSpeed * currentSpeed){
				--carsOnRoads[current][next];
				++numThrough;
				x = point[next].x;
				y = point[next].y;
				next = way[numThrough];
				current = way[numThrough - 1];
				if (numThrough != length){
					++carsOnRoads[current][next];
					g1.setColor(Color.GREEN);
					g1.fillOval((int) x, (int) y, 5, 5);
				}
			} else {
				tangens = getTangens(next, x, y);
				if(tangens[1] == 0.0) {
					dx = 0;
					y += currentSpeed * tangens[2];
				} else {
					dx = currentSpeed * tangens[1] * Math.sqrt(1 / (1 + tangens[0] * tangens[0]));
					x += dx;
					y += tangens[0] * dx;
				}
				if (numThrough != length){
					g1.setColor(Color.GREEN);
					g1.fillOval((int) x, (int) y, 5, 5);
				}
			}
		} else {
			FlagDeleted = 1;
		}
	}

	public int getFlagDeleted() {
		return FlagDeleted;
	}
	public static void setSpeed( double speed){
		SingleCar.speed = speed;
		
	}
}